package com.example.indenture.company.controller;

import com.example.indenture.common.service.ImageService;
import com.example.indenture.common.vo.Result;
import com.example.indenture.common.vo.Search;
import com.example.indenture.company.entity.Company;
import com.example.indenture.company.service.CompanyService;
import com.example.indenture.race.entity.Race;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ImageService imageService;

    @PostMapping(value = "/company/insert",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Company> insertCompany(@RequestBody Company company,HttpSession httpSession){
        return companyService.insertCompany(company,httpSession);
    }

    @PutMapping(value = "/company/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Company>updateCompany(@RequestBody Company company){
        return companyService.updateCompany(company);
    }

    @PostMapping(value = "/company/login",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Company>loginCompany(@RequestBody Company company, HttpSession httpSession){
        System.out.println(company);
        return companyService.loginCompany(company.getCompanyName(),company.getPassword(),httpSession);
    }

    @PostMapping(value = "/companies",consumes = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<Company> getCompanyBySearch(@RequestBody Search search){
        return companyService.getCompaniesBySearch(search);
    }

    @RequestMapping("/company/getById/{id}")
    public Company getRaceById(@PathVariable Integer id) {
        return companyService.getCompanyById(id);
    }

    @RequestMapping(value = "/company/image/{imageTypeName}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
    public Result<String>uploadImage(  @RequestParam MultipartFile mf, @RequestParam Integer id, @PathVariable String imageTypeName) {
        Result<String>result = imageService.uploadImage(mf, imageTypeName);
        String url =  result.getData();
        companyService.updateCompanyUrl(id, url);
        return result;
    }

}

